package com.technology.education.ui.student.withdraw

import android.os.Bundle
import android.widget.ImageView
import androidx.core.os.bundleOf
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.technology.education.R
import com.technology.education.data.SpKeys
import com.technology.education.databinding.FragmentWithdrawStudentBinding
import com.technology.education.ext.*
import com.technology.education.ui.common.information.CompleteWithdrawActivity
import me.hgj.jetpackmvvm.base.fragment.BaseVmDbFragment
import me.hgj.jetpackmvvm.ext.parseState
import me.hgj.jetpackmvvm.util.startActivity

/**
 * 只用于学生积分提现页面
 */
class WithdrawStudentFragment :
    BaseVmDbFragment<WithdrawStudentViewModel, FragmentWithdrawStudentBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolBar.tvTitle.text = "积分提现"
        mDatabind.vm = mViewModel
        mDatabind.ivCode.setOnClickListener { view ->
            selectPhoto(selectedList = arrayListOf()) {
                if (it.isNotEmpty()) {
                    mViewModel.photoPath = it[0]
                    loadImage(mViewModel.photoPath!!.path, view as ImageView)
                }
            }
        }
        mViewModel.getData()
    }

    override fun createObserver() {
        mViewModel.applyResult.observe(this) {
            dismissLoading()
            parseState(it, onSuccess = { it ->
                toast("我们将在七个工作日内处理您的申请")
                navigation(
                    requireView(), R.id.action_withdrawStudentFragment_to_withDrawDetailFragment,
                    bundleOf("id" to it.id, "type" to 3)
                )
            }, onError = { err -> toast(err.errorMsg) })
        }

        mViewModel.showLoading.observe(this) {
            if (it) {
                showLoading()
            }
        }
    }

    override fun showLoading(message: String) {
        showLoadingExt()
    }

    override fun dismissLoading() {
        dismissLoadingExt()
    }
}